﻿#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Premiere.DTO;
using System.Collections;
using Premiere.Window;

#endregion

namespace PremiereMX
{
    public class GeneralSetUpLogic
    {
        GeneralSetUpDTO m_subject;
        IMainForm m_mainForm;
        IUserControlGeneralSetUp m_form;
        IList m_currencyList;
        IList m_periodList;
        EditMode m_editMode = EditMode.OK;
        IList m_autoNumberSetupList = new ArrayList();

        public GeneralSetUpLogic(IMainForm mainForm, IUserControlGeneralSetUp form)
        {
            m_mainForm = mainForm;
            m_form = form;
        }
        public void Init()
        {
            m_subject = m_mainForm.GetGeneralSetup();
            m_currencyList = ServiceRegistry.GetInstance().GetCurrencyService().LoadAll();
            m_periodList = ServiceRegistry.GetInstance().GetPeriodService().LoadAll();
            m_autoNumberSetupList = ServiceRegistry.GetInstance().GetAutoNumberSetupService().LoadAll();
            updateForm();
            m_form.SetToReadOnly(true);
            SetButtonEditMode(EditMode.OK);
        }

        private void updateForm()
        {
            m_form.AllowMinus = m_subject.AllowMinus;
            m_form.BaseCurrency = m_subject.BaseCurrency  ;
            m_form.ChangeLatestPurchasePrice = m_subject.ChangeLatestPurchasePrice;
            m_form.CompanyAddress = m_subject.CompanyAddress;
            m_form.CompanyCity = m_subject.CompanyCity;
            m_form.CompanyEmail = m_subject.CompanyEmail;
            m_form.CompanyFax = m_subject.CompanyFax;
            m_form.CompanyPhone = m_subject.CompanyPhone;
            m_form.CompanyRegDate = m_subject.CompanyRegDate;
            m_form.CompanyTax = m_subject.CompanyTax;
            m_form.CompanyWebsite = m_subject.CompanyWebsite;
            m_form.CompName = m_subject.CompanyName;
            m_form.ComputerName = m_subject.ComputerName;
            m_form.CostMethod = m_subject.CostMethod;
            m_form.CustomerCategory = m_subject.CustomerCategory;
            m_form.CustomerCategory1 = m_subject.CustomerCategory1;
            m_form.CustomerCategory1ColumnName = m_subject.CustomerCategory1ColumnName;
            m_form.CustomerCategory2 = m_subject.CustomerCategory2;
            m_form.CustomerCategory2ColumnName = m_subject.CustomerCategory2ColumnName;
            m_form.CustomerCategory3 = m_subject.CustomerCategory3;
            m_form.CustomerCategory3ColumnName = m_subject.CustomerCategory3ColumnName;
            m_form.CustomerToString = m_subject.CustomerToString;
            m_form.DuplicatePartWarning = m_subject.DuplicatePartWarning;
            m_form.ExchangeRateCurrency = m_subject.ExchangeRateCurrency;
            m_form.HaveCashierInTransaction = m_subject.HaveCashierInTransaction;
            m_form.LastUpdate = m_subject.LastUpdate;
            m_form.MinusStockWarning = m_subject.MinusStockWarning;
            m_form.MultiDivision = m_subject.MultiDivision;
            m_form.MultiWarehouse = m_subject.MultiWarehouse;
            m_form.PartCategory = m_subject.PartCategory;
            m_form .PartCategory1= m_subject.PartCategory1;
            m_form.PartCategory1ColumnName = m_subject.PartCategory1ColumnName;
            m_form.PartCategory2 = m_subject.PartCategory2;
            m_form.PartCategory2ColumnName = m_subject.PartCategory2ColumnName;
            m_form.PartCategory3 = m_subject.PartCategory3;
            m_form.PartCategory3ColumnName = m_subject.PartCategory3ColumnName;
            m_form.PartCurrencyControl = m_subject.PartCurrencyControl;
            m_form.PartToString = m_subject.PartToString;
            m_form.PurchaseBonus = m_subject.PurchaseBonus;
            m_form.PurchaseDiscountAmountByPart = m_subject.PurchaseDiscountAmountByPart;
            m_form.PurchaseDiscountAmountByTotal = m_subject.PurchaseDiscountAmountByTotal;
            m_form.PurchaseDiscountPercentByPart = m_subject.PurchaseDiscountPercentByPart;
            m_form.PurchaseDiscountPercentByTotal = m_subject.PurchaseDiscountPercentByTotal;
            m_form.PurchaseOtherAmount = m_subject.PurchaseOtherAmount;
            m_form.PurchaseTax = m_subject.PurchaseTax;
            m_form.SalesBonus = m_subject.SalesBonus;
            m_form.SalesDiscountAmountByPart = m_subject.SalesDiscountAmountByPart;
            m_form.SalesDiscountAmountByTotal = m_subject.SalesDiscountAmountByTotal;
            m_form.SalesDiscountPercentByPart = m_subject.SalesDiscountPercentByPart;
            m_form.SalesDiscountPercentByTotal = m_subject.SalesDiscountPercentByTotal;
            m_form.SalesOtherAmount = m_subject.SalesOtherAmount;
            m_form.SalesTax = m_subject.SalesTax;
            m_form.StartEntryPeriod = m_subject.StartEntryPeriod;
            m_form.SupplierCategory = m_subject.SupplierCategory;
            m_form.SupplierCategory1 = m_subject.SupplierCategory1;
            m_form.SupplierCategory1ColumnName = m_subject.SupplierCategory1ColumnName;
            m_form.SupplierCategory2 = m_subject.SupplierCategory2;
            m_form.SupplierCategory2ColumnName = m_subject.SupplierCategory2ColumnName;
            m_form.SupplierCategory3 = m_subject.SupplierCategory3;
            m_form.SupplierCategory3ColumnName = m_subject.SupplierCategory3ColumnName;
            m_form.UserName = m_subject.UserName;
            foreach (AutoNumberSetupDTO adto in m_autoNumberSetupList)
            {
                m_form.AddAutoNumberSetup(adto);
            }
        }
        private void updateSubject()
        {
            m_subject.AllowMinus = m_form.AllowMinus;
            m_subject.BaseCurrency = m_form.BaseCurrency;
            m_subject.ChangeLatestPurchasePrice = m_form.ChangeLatestPurchasePrice;
            m_subject.CompanyAddress = m_form.CompanyAddress;
            m_subject.CompanyCity = m_form.CompanyCity;
            m_subject.CompanyEmail = m_form.CompanyEmail;
            m_subject.CompanyFax = m_form.CompanyFax;
            m_subject.CompanyName = m_form.CompName;
            m_subject.CompanyPhone = m_form.CompanyPhone;
            m_subject.CompanyRegDate = m_form.CompanyRegDate;
            m_subject.CompanyTax = m_form.CompanyTax;
            m_subject.CompanyWebsite = m_form.CompanyWebsite;
            m_subject.CostMethod = m_form.CostMethod;
            m_subject.CustomerCategory = m_form.CustomerCategory;
            m_subject.CustomerCategory1 = m_form.CustomerCategory1;
            m_subject.CustomerCategory1ColumnName = m_form.CustomerCategory1ColumnName;
            m_subject.CustomerCategory2 = m_form.CustomerCategory2;
            m_subject.CustomerCategory2ColumnName = m_form.CustomerCategory2ColumnName;
            m_subject.CustomerCategory3 = m_form.CustomerCategory3;
            m_subject.CustomerCategory3ColumnName = m_form.CustomerCategory3ColumnName;
            m_subject.CustomerToString = m_form.CustomerToString;
            m_subject.DuplicatePartWarning = m_form.DuplicatePartWarning;
            m_subject.ExchangeRateCurrency = m_form.ExchangeRateCurrency;
            m_subject.HaveCashierInTransaction = m_form.HaveCashierInTransaction;
            m_subject.LastUpdate = DateTime.Now;
            m_subject.MinusStockWarning = m_form.MinusStockWarning;
            m_subject.MultiDivision = m_form.MultiDivision;
            m_subject.MultiWarehouse = m_form.MultiWarehouse;
            m_subject.PartCategory = m_form.PartCategory;
            m_subject.PartCategory1 = m_form.PartCategory1;
            m_subject.PartCategory1ColumnName = m_form.PartCategory1ColumnName;
            m_subject.PartCategory2 = m_form.PartCategory2;
            m_subject.PartCategory2ColumnName = m_form.PartCategory2ColumnName;
            m_subject.PartCategory3 = m_form.PartCategory3;
            m_subject.PartCategory3ColumnName = m_form.PartCategory3ColumnName;
            m_subject.PartCurrencyControl = m_form.PartCurrencyControl;
            m_subject.PartToString = m_form.PartToString;
            m_subject.PurchaseBonus = m_form.PurchaseBonus;
            m_subject.PurchaseDiscountAmountByPart = m_form.PurchaseDiscountAmountByPart;
            m_subject.PurchaseDiscountAmountByTotal = m_form.PurchaseDiscountAmountByTotal;
            m_subject.PurchaseDiscountPercentByPart = m_form.PurchaseDiscountPercentByPart;
            m_subject.PurchaseDiscountPercentByTotal = m_form.PurchaseDiscountPercentByTotal;
            m_subject.PurchaseOtherAmount = m_form.PurchaseOtherAmount;
            m_subject.PurchaseTax = m_form.PurchaseTax;
            m_subject.SalesBonus = m_form.SalesBonus;
            m_subject.SalesDiscountAmountByPart = m_form.SalesDiscountAmountByPart;
            m_subject.SalesDiscountAmountByTotal = m_form.SalesDiscountAmountByTotal;
            m_subject.SalesDiscountPercentByPart = m_form.SalesDiscountPercentByPart;
            m_subject.SalesDiscountPercentByTotal = m_form.SalesDiscountPercentByTotal;
            m_subject.SalesOtherAmount = m_form.SalesOtherAmount;
            m_subject.SalesTax = m_form.SalesTax;
            m_subject.SupplierCategory = m_form.SupplierCategory;
            m_subject.SupplierCategory1 = m_form.SupplierCategory1;
            m_subject.SupplierCategory1ColumnName = m_form.SupplierCategory1ColumnName;
            m_subject.SupplierCategory2 = m_form.SupplierCategory2;
            m_subject.SupplierCategory2ColumnName = m_form.SupplierCategory2ColumnName;
            m_subject.SupplierCategory3 = m_form.SupplierCategory3;
            m_subject.SupplierCategory3ColumnName = m_form.SupplierCategory3ColumnName;
            m_subject.UserName = m_mainForm.GetUser().Code;
            m_subject.ComputerName = Environment.MachineName;
            m_subject.StartEntryPeriod = m_form.StartEntryPeriod;
        }
        public IList GetCurrencyList()
        {
            return m_currencyList;
           
        }
        public IList GetPeriodList()
        {
            return m_periodList;
        }
        public void Refresh()
        {
            //m_currencyList = ServiceRegistry.GetInstance().GetCurrencyService().LoadAll();
            //m_periodList = ServiceRegistry.GetInstance().GetPeriodService().LoadAll();
        }
        protected virtual void SetButtonEditMode(EditMode editMode)
        {
            m_form.SetButtonEditMode(editMode);
            m_editMode = editMode;
        }
        public bool GetStockCardEntryCount()
        {
            return ServiceRegistry.GetInstance().GetPeriodService().GetStockCardEntryCount() > 0;
        }
        public void Edit()
        {
            m_form.SetToReadOnly(false);
            m_form.SetButtonEditMode(EditMode.Insert);
        }
        public void Save()
        {
            try
            {
                updateSubject();
                IList autonumbering = m_form.UpdateAutoNumberItems();
                foreach(AutoNumberSetupDTO adto in autonumbering)
                {
                    ServiceRegistry.GetInstance().GetAutoNumberSetupService().Save(adto);
                }
                m_subject.Id = ServiceRegistry.GetInstance().GetGeneralSetupService().Save(m_subject);
                FormFactory.GetInstance().SetSetup(m_subject as GeneralSetUpDTO);
                //getGeneralSetUpForm.UserUpdateBy = User.Code; //update last update user
                //getGeneralSetUpForm.ComputerName = Environment.MachineName;
                //getGeneralSetUpForm.LastUpdate = DateTime.Now;
                m_form.ShowMessage("Record has been saved!");
                m_form.SetToReadOnly(true);
                SetButtonEditMode(EditMode.OK);
            }
            catch (Exception ex)
            {
                m_form.ShowMessage(ex.Message);
            }
        }
    }
}